<?php 
set_time_limit(0);
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
require './c/config.php';
require_once './data/Classes/PHPExcel.php';

$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', '名字')
->setCellValue('B1', '手机号')
->setCellValue('C1', '座次')
->setCellValue('D1', 'GPS')
->setCellValue('E1', 'IP')
->setCellValue('F1', '地理位置')
->setCellValue('G1', '参与时间')
;

 
$mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
if (!$mysqli->set_charset("utf8")) {printf("Error loading character set utf8: %s\n", $mysqliTwo->error);}
$sql="SELECT * FROM `uinfo` order by id ";
$query =$mysqli->query($sql);
$index=2;
while ($list=$query->fetch_array()){
    
    $sql="SELECT name,zuoci FROM `ud` where phone='".$list['phone']."' limit 0,1 ";
    $queryU=$mysqli->query($sql);
    $getU=$queryU->fetch_assoc();
    
    $sql="SELECT dizhi FROM `users` where openid='".$list['openid']."' limit 0,1 ";
    $queryD=$mysqli->query($sql);
    $getD=$queryD->fetch_assoc();

    $objPHPExcel->setActiveSheetIndex(0)
    ->setCellValue('A'.$index, $getU['name'])
    ->setCellValue('B'.$index, $list['phone'])
    ->setCellValue('C'.$index, $getU['zuoci'])
    ->setCellValue('D'.$index, $list['gps'])
    ->setCellValue('E'.$index, $list['ip'])
    ->setCellValue('F'.$index, $getD['dizhi'])
    ->setCellValue('G'.$index, $list['createtime'])
    ;
    $index++;
}

// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('用户信息');


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="用户信息.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
//$objWriter->save();
exit;